﻿using System;
using System.Collections.Specialized;
using System.IO;

using WebDavProcessor.Interfaces;
using WebDavProcessor.Methods;

namespace WebDavProcessor
{
    public class WebDavProcessor
    {
        public IWebDavResourceProvider dataProvider;

        public WebDavProcessor(IWebDavResourceProvider resourceProvider)
        {
            dataProvider = resourceProvider;
        }

        public IWebDavResponse ProcessRequest(string requestMethod, Uri requestUri, NameValueCollection requestHeaders, Stream requestInputStream)
        {
            WebDavMethod method = null;
            switch (requestMethod)
            {
                case "GET":
                    method = new WebDavGet(requestMethod, requestUri, requestHeaders, requestInputStream);
                    break;
                case "HEAD":
                    method = new WebDavHead(requestMethod, requestUri, requestHeaders, requestInputStream);
                    break;
                case "PUT":
                    method = new WebDavPut(requestMethod, requestUri, requestHeaders, requestInputStream);
                    break;
                case "DELETE":
                    method = new WebDavDelete(requestMethod, requestUri, requestHeaders, requestInputStream);
                    break;
                case "PROPFIND":
                    method = new WebDavPropFind(requestMethod, requestUri, requestHeaders, requestInputStream);
                    break;
            }
            if (method != null)
            {
                return method.ProcessRequest(this.dataProvider);
            }
            return null;
        }

        public IWebDavResponse ProcessRequest(IWebDavRequest request)
        {
            return ProcessRequest(request.Method, request.RequestUri, request.Headers, request.InputStream);
        }
    }
}
